package com.geek.novel.view.button

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.CountDownTimer
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatButton
import com.geek.novel.R
import com.geek.novel.utils.ToastUtils

/**倒计时按钮*/
class CountDownButton: AppCompatButton, View.OnClickListener {

    /**倒计时时长*/
    private var totalCountDownTime = 10

    /**倒计时完毕后的文案*/
    private var resetText: String? = null


    /**默认的背景*/
    private var defaultBackground: Drawable? = null

    /**倒计时中的背景*/
    private var countDownBackground: Drawable? = null

    /**倒计时定时任务对象*/
    private var countDownTimer: CountDownTimer? = null


    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context, attrs, defStyleAttr)
    }


    /**
     * 初始化UI组件
     */
    private fun init(context: Context, attrs: AttributeSet?, defStyleAttr: Int=0) {
        //初始化属性
        this.initAttrs(attrs, defStyleAttr)

        val time = totalCountDownTime * 1000L
        //初始化倒计时控件
        this.countDownTimer = CircularTimer(this, time, 1000L)

        //绑定点击事件
        this.setOnClickListener(this)
    }


    /**初始化属性*/
    private fun initAttrs(attrs: AttributeSet?, defStyleAttr: Int=0) {
        this.defaultBackground = this.background
        if(attrs != null){
            //获取属性
            val array = context.obtainStyledAttributes(attrs, R.styleable.CountDownButton, defStyleAttr, 0)
            countDownBackground = array.getDrawable(R.styleable.CountDownButton_countDownBackground)
            resetText = array.getString(R.styleable.CountDownButton_resetText)
            totalCountDownTime = array.getInt(R.styleable.CountDownButton_countDownTime, 10)
            array.recycle()
        }
        if(TextUtils.isEmpty(resetText)){
            this.resetText = this.text.toString()
        }
    }

    /**点击事件*/
    override fun onClick(v: View?) {
        try {
            if(this.listener != null){
                //触发点击事件
                listener!!.onClick(v)
                //禁用按钮
                this.isEnabled = false
                //开始倒计时
                this.countDownTimer!!.start()
                //设置背景
                if(this.countDownBackground != null){
                    this.background = countDownBackground
                }
            }
        } catch (e: Exception) {
            ToastUtils.toast(e.message)
        }
    }

    /**倒计时*/
    private class CircularTimer(val btn: CountDownButton, mTotalTime: Long, updateInterval: Long) : CountDownTimer(mTotalTime, updateInterval) {
        override fun onTick(millisUntilFinished: Long) {
            btn.text = "${millisUntilFinished/1000}"
        }
        override fun onFinish() {
            //重新设置按钮状态
            btn.resetBtnStatus()
        }
    }

    /**重新设置按钮状态*/
    private fun resetBtnStatus(){
        this.text = this.resetText
        this.isEnabled = true
        if(this.defaultBackground != null){
            this.background = this.defaultBackground
        }
    }

    /**取消倒计时*/
    fun cancelCountDown(){
        if(this.countDownTimer != null){
            this.countDownTimer!!.cancel()
        }
        //重新设置按钮状态
        this.resetBtnStatus()
    }

    /**倒计时按钮点击事件*/
    private var listener: OnClickListener? = null
    /**设置倒计时按钮点击事件*/
    fun setOnCountDownClickListener(l: OnClickListener?) {
        this.listener = l
    }

    /**销毁组件，关闭定时器*/
    fun onDestroy() {
        if(this.countDownTimer != null){
            this.countDownTimer!!.cancel()
        }
    }

}